/***************************************************************************
 *   Copyleft 2008-2009 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#ifndef __VMATERIALPARSER_H__
#define __VMATERIALPARSER_H__

#include "../FileIO/vTxtInputStream.h"
#include "./vMaterial.h"
#include "../../Macros/vMemoryMacros.h"

namespace Vendetta {
	typedef vHashMap<vString, vString> MaterialInfo;

	/*! \class vMaterialParser
	 *   \brief A vMaterialParser class manages loading and parsing material (.vmt) files.
	 *
	 */
	class vMaterialParser: public vTxtInputStream {
	public:
		/*! \brief Default constructor.
		 *
		 */	
		vMaterialParser();

		/*! \brief Destructor.
		 *
		 */	
		virtual ~vMaterialParser();

		/*! \brief Overloaded from vTxtInputStream.
		 *
		 *	\sa vTxtInputStream.
		 */
		virtual vBool LoadFromFile(const vString& fileName);

	protected:
		/*! \brief Implemented from vTxtInputStream.
		 *
		 *	\sa vTxtInputStream.
		 */
		virtual vVoid postLineLoad(const vString& fileName, const vString& line);

	private:
		enum eLoadState {
			VLS_DEFAULT,
			VLS_MATERIAL_LINE,
			VLS_MATERIAL_OPEN_BRACKET,
			VLS_MATERIAL_CLOSE_BRACKET,
			VLS_MATERIAL_IN,
			VLS_PASS_LINE,
			VLS_PASS_OPEN_BRACKET,
			VLS_PASS_CLOSE_BRACKET,
			VLS_PASS_IN,
			VLS_TEXTURE_LINE,
			VLS_TEXTURE_OPEN_BRACKET,
			VLS_TEXTURE_CLOSE_BRACKET,
			VLS_TEXTURE_IN,
		};

	private:
		vVoid parseMaterial(const vString& fileName, const vArray<vString>& args);

		vVoid parsePass(const vString& fileName, const vArray<vString>& args);

		vVoid parsePassIn(const vString& fileName, const vArray<vString>& args);

		vVoid parseTextureUnit(const vString& fileName, const vArray<vString>& args);

		vVoid parseTextureUnitIn(const vString& fileName, const vArray<vString>& args);

		vBool parseBoolParam(const vString& fileName, const vString& val);

		vReal parseRealParam(const vString& fileName, const vString& val);

		vColor parseLightComponent(const vString& fileName, const vString& val);

		vReal parseSpecularFactor(const vString& fileName, const vString& val);

		vTextureUnit::eAddressMode parseAddressMode(const vString& fileName, const vString& val);

		vTextureUnit::eFilterMode parseFilterMode(const vString& fileName, const vString& val);

		vTextureUnit::eBlendMode parseBlendMode(const vString& fileName, const vString& val);

		vBool changeOpenState(eLoadState eState);

		vBool changeCloseState(eLoadState eState);

	private:
		vMaterial* mpTempMaterial;
		vPass* mpPass;
		vTextureUnit* mpTextureUnit;
		vStack<eLoadState> mStates;
		vInt mLineInFile;

	};
}

#endif // __VMATERIALPARSER_H__